using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;

public class Cell : MonoBehaviour,IPointerClickHandler
{
    [SerializeField]private int cellMineText;
    public int CellMineText
    {
        get
        {
            return cellMineText;
        }
        set
        {
            cellMineText = value;
            if (mineText)
            {
                mineText.text = value.ToString();
                // if (value == 0)
                // {
                //     mineText.gameObject.SetActive(false);
                // }
                // else
                // {
                //     mineText.gameObject.SetActive(true);
                // }
            }
        }
    }
    [SerializeField]private bool isMine;
    public bool IsMine
    {
        get
        {
            return isMine;
        }
        set
        {
            isMine = value;
            mineIcon.gameObject.SetActive(isMine);
        }
    }
    [SerializeField]private bool isFlip;

    public bool IsFlip
    {
        get
        {
            return isFlip;
        }
        set
        {
            isFlip = value;
            //flipIcon.gameObject.SetActive(!value);
            canvasGroupForFilp.DOKill();
            if (value)
            {
                DOTween.To(() => canvasGroupForFilp.alpha, x => canvasGroupForFilp.alpha = x, 0, 1f);
            }
            else
            {
                canvasGroupForFilp.alpha = 1;
                //DOTween.To(() => canvasGroupForFilp.alpha, x => canvasGroupForFilp.alpha = x, 1, 1f);
            }
        }
    }
    [SerializeField]private bool isHide;

    public bool IsHide
    {
        get
        {
            return isHide;
        }
        set
        {
            isHide = value;
            if(hideIcon)
                hideIcon.gameObject.SetActive(value);
        }
    }
    public int index;
    //public Vector2 pos;

    public Image flipIcon;
    public Image hideIcon;
    public Image mineIcon;
    public Image exploIcon;
    public Text mineText;
    public CanvasGroup canvasGroupForFilp;
    public CanvasGroup CanvasGroupForMine;

    // public Cell(int mineNums, bool isMine, bool isFlip,Vector2 pos)
    // {
    //     this.mineNums = mineNums;
    //     this.isMine = isMine;
    //     this.isFlip = isFlip;
    //     this.pos = pos;
    // }
    
    

    public void OnClickLeft()
    {
        GameManager.Instance.gameController.OnClickLeft(this);
    }

    public void OnClickRight()
    {
        GameManager.Instance.gameController.OnClickRight(this);
    }

    public void DoClickLeft()
    {
        IsFlip = true;
    }

    public void DoClickRight()
    {
        //if (isFlip) return;
        if (IsHide)
        {
            //TODO
            //change image
            hideIcon.gameObject.SetActive(false);
            flipIcon.gameObject.SetActive(true);
            IsHide = false;
            return;
        }
        
        IsHide = true;
        //TODO
        //change image
        hideIcon.gameObject.SetActive(true);
        //GameManager.Instance.gameController.mineNum--;
    }

    public void OnPointerClick(PointerEventData eventData)
    {

        if (GameManager.Instance.gameController.firstClick)
        {
            GameManager.Instance.gameController.FirstClick(index,eventData.position);
            GameManager.Instance.gameController.firstClick = false;
        }
        
        //GameManager.Instance.recardStep.Add(index);
        
        if (GameManager.Instance.gameController.isLeft)
        {
            OnClickLeft();
            GameManager.Instance.recardStep.Add(index);
            GameManager.Instance.stepLeftOrRight.Add(true);
            Debug.LogFormat("Click Cell Left    CellIndex : {0}  ,  CellPos : {1} , PointerPos : {2}", index , transform.localPosition,eventData.position);
        }
        else
        {
            if (GameManager.Instance.recardStep.Count > 0 &&
                index != GameManager.Instance.recardStep[GameManager.Instance.recardStep.Count - 1])
            {
                GameManager.Instance.recardStep.Add(index);
                GameManager.Instance.stepLeftOrRight.Add(false);
            }
            
            OnClickRight();
            Debug.LogFormat("Click Cell Right    CellIndex : {0}", index);
        }
    }

    public void ShowMine()
    {
        IsMine = true;
        CanvasGroupForMine.DOKill();
        DOTween.To(() => CanvasGroupForMine.alpha, x => CanvasGroupForMine.alpha = x, 1, 1f);
    }

    public void HideMine()
    {
        IsMine = false;
        CanvasGroupForMine.DOKill();
        CanvasGroupForMine.alpha = 0;
    }

    public void ShowExplo()
    {
        exploIcon.gameObject.SetActive(true);
    }

    public void HideExplo()
    {
        exploIcon.gameObject.SetActive(false);
    }
    
}
